﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmAddNavigation : MeForm
    {
        public frmAddNavigation()
        {
            InitializeComponent();
            cboMenu.DisplayMember = "Title";
            cboMenu.ValueMember = "Id";
            cboMenu.DataSource = DataFactory.Menus.Nodes;
        }
        public frmAddNavigation(UserConfig config):this()
        {
            txtMessage.Text = config.Alias;
            txtMessage.Enabled = false;
            cboMenu.SelectedValue = config.GuideMenu;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMessage.Text))
            {
                MeMessageBox.ShowInformation("旺旺帐号不能为空!");
                return;
            }
            var d = from temp in DataFactory.SystemConfig.UserConfigs where temp.Alias.Equals(txtMessage.Text) select temp;
            UserConfig config = d.FirstOrDefault();
            if (config != null)
            {
                config.GuideMenu = int.Parse(cboMenu.SelectedValue.ToString());
            }
            else
            {
                config = new UserConfig();
                config.Alias = txtMessage.Text;
                config.Account = config.Alias;
                config.GuideMenu = int.Parse(cboMenu.SelectedValue.ToString());
                config.WorkMode = TalkerState.NoLicense;
                DataFactory.SystemConfig.UserConfigs.Add(config);
            }
            DialogResult = DialogResult.Yes;
            this.Close();
        }
    }
}